package com.android.marble;

/**
 * Marble.java
 * Define the Marble.
 *
 * @author Daniel Deng
 * @create 6-28-2012
 * @update 6-30-2012
 */
import com.badlogic.gdx.math.Vector2;
import com.stickycoding.rokon.Sprite;
import com.stickycoding.rokon.Texture;

public class Marble extends Sprite{

	private float mRadius;
	private float mSpeed;
	private float mWorldWidth,mWorldHeight;
	private Plate mPlate;
	
	private boolean bumpLeft   = false; 
	private boolean bumpRight  = false;
	private boolean bumpPlate  = false;
	private boolean bumpTop    = false;
	

	public Marble(float x, float y, float radius, float worldWidth, float worldHeight, Plate plate) {
		super(x, y, 2*radius, 2*radius);
		mRadius = radius;
		mWorldWidth = worldWidth;
		mWorldHeight = worldHeight;
		mPlate = plate;
	}	
	/**
	 * Create a marble with initial velocity, speed,and texture resource.
	 * 
	 * @param velocity Vector
	 * @param speed float
	 * @param texture Texture
	 */
	public void creat(Vector2 velocity,float speed,Texture texture){
		//CircleShape circle = new CircleShape();
		//circle.setRadius(mRadius);
		//circle.dispose();
		
		mSpeed = speed;
		
		velocity.nor();
		velocity.x *= mSpeed;
		velocity.y *= mSpeed;
		setTexture(texture);	
	}
	/**
	 * Update the marble's state of motion. .
	 * 
	 * @param velocity Vector
	 */
	public void update(Vector2 velocity){

		if((getCenterPosition().x - mRadius) <= 0)  bumpLeft = true;
		else bumpLeft  = false;
		if((getCenterPosition().x + mRadius) >= mWorldWidth)  bumpRight = true;
		else bumpRight = false;
		if((getCenterPosition().y + mRadius) >= mWorldHeight) bumpPlate=true;
		else bumpPlate=false;
		if((getCenterPosition().y - mRadius) <= 0)  bumpTop = true;
		else bumpTop = false;
		velocity.nor();	
		
		velocity.x *= mSpeed;
		velocity.y *= mSpeed;
		
		if(bumpLeft){
			velocity.x = -velocity.x;
		}
		else if(bumpRight){
			velocity.x = -velocity.x;	
		}
		else if(bumpPlate){
			velocity.y = -velocity.y;
			
		}
		else if(bumpTop){
			velocity.y = -velocity.y;
		}
		
		this.setVelocity(velocity.x,velocity.y);
		onUpdate();	
		verifyPosition();

	}	
	
	/**
	 * The marble becames larger
	 * 
	 */
	public void Largen(){
		
	}
	/**
	 * Sets the speed of the DynamicObject on both X and Y axis
	 * 
	 * @param x positive or negative floating point
	 * @param y positive or negative floating point
	 */
	@Override
	public void setVelocity(float x, float y) {
		super.setSpeedX(x);
		super.setSpeedY(y);
	}
	/**
	 * Set the  position of Marble
	 * 
	 * @param x 
	 * @param y
	 */
	public void setPosition(float x, float y){
		super.x = x-mRadius;
		super.y = y-mRadius;
	}
	/**
	 * return the position of Marble
	 *
	 */
	public Vector2 getCenterPosition(){
		Vector2 pos = new Vector2(super.x+mRadius,super.y+mRadius);
		return pos;
	}
	public float getPointToCenterDistance(float x, float y){
		return (float)(Math.sqrt(Math.pow(x-getCenterPosition().x,2)+(Math.pow(y-getCenterPosition().y,2))));
	}
	private void verifyPosition(){
		float oX=getCenterPosition().x;
		float oY=getCenterPosition().y;
		if(oX - mRadius < 0)oX = mRadius;
		if(oX + mRadius > mWorldWidth)oX=mWorldWidth-mRadius;
		if(oY - mRadius < 0)oY = mRadius;
		if(oY + mRadius > mWorldHeight)oY = mWorldHeight-mRadius;
		setPosition(oX,oY);
	}
}
